﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Acoris.Foundation
{
    /// <summary>
    /// 
    /// </summary>
    public struct Boundary:IEqualityComparer<Boundary>
    {
        private float m_x;
        private float m_y;
        private float m_width;
        private float m_height;

        // ⑨

        #region Constructor

        public Boundary(float x,float y,float width,float height)
        {
            m_x = x;
            m_y = y;
            m_width = width;
            m_height = height;
        }

        public Boundary(float x, float y, Size size):this(x,y,size.Width,size.Height) { }

        public Boundary(Point point, float width, float height):this(point.X,point.Y,width,height) { }

        public Boundary(Point point,Size size) : this(point.X, point.Y, size.Width, size.Height) { }

        #endregion

        public float X {
            get { return m_x; }
            set { m_x = value; }
        }

        public float Y {
            get { return m_y; }
            set { m_y = value; }
        }

        public float Width {
            get { return m_width; }
            set { m_width = value; }
        }

        public float Height {
            get { return m_height; }
            set { m_height = value; }
        }

        public void CenterPointTranslation(Point point)
        {
            m_x += point.X;
            m_y += point.Y;
        }

        #region IEqualityComparer<Boundary>接口成员

        public bool Equals(Boundary x,Boundary y)
        {
            return x == y;
        }

        public int GetHashCode(Boundary boundary)
        {
            return boundary.GetHashCode();
        }

        #endregion

        #region Override

        /// <summary>
        /// 获取与当前对象等价的字符串形式
        /// </summary>
        /// <returns>返回与当前对象等价的字符串形式</returns>
        public override string ToString()
        {
            return $"x:{m_x},y:{m_y},h:{m_height},w:{m_width}";
        }

        /// <summary>
        /// 获取当前对象与指定对象之间的等价性
        /// </summary>
        /// <param name="obj">指定要比较的对象</param>
        /// <returns>如果两个对象之间等价则返回true，否则返回false</returns>
        public override bool Equals(object obj)
        {
            if(obj is Boundary boundary)
            {
                return this.m_width == boundary.m_width &&
                       this.m_height == boundary.m_height &&
                       this.m_x == boundary.m_x &&
                       this.m_y == boundary.m_y;
            }
            return false;
        }

        /// <summary>
        /// 获取当前对象的哈希值
        /// </summary>
        /// <returns>返回当前对象的哈希值</returns>
        public override int GetHashCode()
        {
            return (((int)m_x + (int)m_y) << 32) + (((int)m_width+(int)m_height));
        }
        #endregion

        #region operator == / !=

        public static bool operator !=(Boundary x, Boundary y)
        {
            return x.m_width != y.m_width ||
                   x.m_height != y.m_height ||
                   x.m_x != y.m_x ||
                   x.m_y != y.m_y;
        }

        public static bool operator ==(Boundary x, Boundary y)
        {
            return x.m_width == y.m_width &&
                   x.m_height == y.m_height &&
                   x.m_x == y.m_x &&
                   x.m_y == y.m_y;
        }

        #endregion
    }
}
